<?php

namespace app\common\model\banxi\learn;

use app\common\enums\AccountTypeEnum;
use app\common\enums\RangeLevelEnum;
use app\common\enums\say\LearnSayTestFullTypeEnum;
use app\common\model\banxi\core\CoreSayVocabularySetting;
use app\common\model\banxi\core\CoreVocabularySetting;
use app\common\model\banxi\learn\LearnSayVocabularyRecord as LearnSayVocabularyRecordModel;
use app\common\model\banxi\organization\Organization as OrganizationModel;
use app\common\model\banxi\say\SayGrade;
use app\common\model\banxi\trainer\Trainer;
use app\common\model\common\BaseModel;
use app\common\service\AccountService;
use think\model\concern\SoftDelete;

/**
 * 词汇量测试记录
 */
class LearnSayVocabularyRecord extends BaseModel
{
    use SoftDelete;

    protected $name = 'banxi_learn_say_vocabulary_record';

    protected $autoWriteTimestamp = true;

    protected $deleteTime = 'del';

    protected $defaultSoftDelete = 0;

    protected $append = [
        'accountTypeCn',
        'testFullTypeCn',
        'scoreDetailList',
        'testDurationTime'
    ];

    protected $type = [
        'id' => 'string',
        'del' => 'string',
        'skRecordId' => 'string',
        'tutorTrainerId' => 'string',
        'trainerId' => 'string',
        'studentId' => 'string',
        'level' => 'string',
        'isSubmit' => 'boolean',
        'createTime' => 'timestamp:Y-m-d H:i:s',
        'updateTime' => 'timestamp:Y-m-d H:i:s',
    ];


    //-----------------------------------------------------------------------------------
    //｜                                   获取器                                       ｜
    //-----------------------------------------------------------------------------------

    public function getAccountTypeCnAttr($value, $data)
    {
        $value = $value ?: ($data['accountType'] ?? '');
        return (new AccountTypeEnum())->getDescription($value) ?? '';
    }
    public function getTestFullTypeCnAttr($value, $data)
    {
        $value = $value ?: ($data['testFullType'] ?? '');
        return (new LearnSayTestFullTypeEnum())->getDescription($value) ?? '';
    }

    public function getScoreDetailListAttr($value, $data)
    {
        $value = $value ?: ($data['scoreDetail'] ?? '');
        return json_decode($value, true);
    }

    public function getTestDurationTimeAttr($value, $data)
    {
        $value = $value ?: ($data['testDuration'] ?? 0);
        return secondToTime($value);
    }


    //-----------------------------------------------------------------------------------
    //｜                                   处理方法                                       ｜
    //-----------------------------------------------------------------------------------


    public function getDetail($recordId)
    {
        $detail = $this->where('id', $recordId)->find();
        if (!$detail) {
            getResponseException('记录不存在', [], 1, 'ERROR');
        }
        $accountId = $detail['accountId'];
        $accountType = $detail['accountType'];
        $accountService = new AccountService();
        $accountInfo = $accountService->getAccountInfo($accountId, $accountType);
        if (in_array($accountType, [AccountTypeEnum::STUDENT, AccountTypeEnum::TRAINER])) {
            $organizationModel = new OrganizationModel();
            if ($accountInfo['organizationId'] == 0) {
                $accountInfo['organizationBaseInfo'] = $organizationModel->getOrganizationInfo();
            } else {
                $accountInfo['organizationBaseInfo'] = $organizationModel->where('id', $accountInfo['organizationId'])->field('id,name,managerName')->find();
            }
        }
        $detail->accountInfo = $accountInfo;
        $coreVocabularySettingModel = new CoreSayVocabularySetting();
        $detail->settingInfo = $coreVocabularySettingModel->where(['id' => $detail['level']])->find();
        return $detail;
    }

    //测试列表
    public function getVocabularyList($where,$page,$limit)
    {
        $coreSayVocabularySettingModel = new CoreSayVocabularySetting();
        $sayGradeModel = new SayGrade();
        $settingList = $coreSayVocabularySettingModel->field('id,rangeName,rangeLevel')->select()->toArray();
        $settingList = array_column($settingList, null, 'id');
        $sayGradeList = $sayGradeModel->column('id,name', 'id');
        return $this->where($where)
            ->field('id,testFullType,score,countWord,level,testDuration,startTime')
            ->order(['id' => 'desc'])
            ->paginate([
                'page' => $page,
                'list_rows' => $limit
            ])->each(function ($item) use ($settingList, $sayGradeList) {
                if ($item['testFullType'] == LearnSayTestFullTypeEnum::E3) {
                    $settingInfo = $settingList[$item['level']];
                    $rangeName = $settingInfo ? $settingInfo['rangeName'] . $settingInfo['rangeLevelCn'] : '';
                    $item['levelName'] = $rangeName;
                    $item['wordLevelName'] = '';
                } else {
                    $settingInfo = $sayGradeList[$item['level']];
                    $rangeName = $settingInfo ? $settingInfo['name'] : '';
                    $wordSettingInfo = $sayGradeList[$item['wordLevel']];
                    $wordRangeName = $wordSettingInfo ? $wordSettingInfo['name'] : '';
                    $item['levelName'] = $rangeName;
                    $item['wordLevelName'] = $wordRangeName;
                }

            });
    }

    //-----------------------------------------------------------------------------------
    //｜                                   模型关联                                       ｜
    //-----------------------------------------------------------------------------------

    public function trainerInfo()
    {
        return $this->hasOne(Trainer::class, 'id', 'trainerId');
    }

    public function studentInfo()
    {
        return $this->hasOne(Trainer::class, 'id', 'studentId');
    }

    public function settingInfo()
    {
        return $this->hasOne(CoreSayVocabularySetting::class, 'id', 'level');
    }
    public function gradeLevel()
    {
        return $this->hasOne(SayGrade::class, 'id', 'level');
    }
    public function wordGradeLevel()
    {
        return $this->hasOne(SayGrade::class, 'id', 'wordLevel');
    }

}